﻿using System;
using System.Windows.Forms;
using CodeSnippet.Config;
using CodeSnippet.Helpers;
using WLWPluginBase.Win32;

namespace CodeSnippet.Forms
{
    /// <summary>
    /// Options dialog for the Code Snippet plugin.
    /// </summary>
    internal partial class CodeSnippetOptionsForm : Form
    {
        #region Properties
        /// <summary>
        /// Plugin configuration.
        /// </summary>
        public CodeSnippetConfig Config { get; private set; }
        #endregion Properties

        #region Initialization
        /// <summary>
        /// Initializes a new instance of the <see cref="CodeSnippetOptionsForm"/> class.
        /// </summary>
        /// <param name="config">The <see cref="CodeSnippetConfig"/> instance with all the configuration details.</param>
        public CodeSnippetOptionsForm(CodeSnippetConfig config)
        {
            Config = config;
            InitializeComponent();
        }
        #endregion Initialization

        #region Event Handlers
        /// <summary>
        /// Handles the Load event of the CodeSnippetOptionsForm control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void CodeSnippetOptionsForm_Load(object sender, EventArgs e)
        {
            // Reposition the dialog to its last recorded position.
            Win32WndHelper.SetPlacement(Handle, Config.Layout.OptionsPlacement);
            // Load the tree view control with the supported sections.
            foreach (IConfigSection section in Config.Sections)
            {
                // Add the icon to the image list.
                ilSections.Images.Add(section.SectionName, section.Image);
                // Use the name as both the key and the text of the nodes.
                lvSections.Items.Add(section.SectionName, section.SectionName);
            }
            // Resize the property grid to make the first column 30% wide instead of the default 50% wide.
            PropertyGridHelper.ResizePropertyGridColumns(pgSectionDetail, 30);
            // Force the first node to be selected.
            lvSections.Items[0].Selected = true;
        }
        /// <summary>
        /// Handles the FormClosing event of the CodeSnippetOptionsForm control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.FormClosingEventArgs"/> instance containing the event data.</param>
        private void CodeSnippetOptionsForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            Config.Layout.OptionsPlacement = Win32WndHelper.GetPlacement(Handle);
        }
        /// <summary>
        /// Handles the ItemSelectionChanged event of the lvSections control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.ListViewItemSelectionChangedEventArgs"/> instance containing the event data.</param>
        private void lvSections_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            pgSectionDetail.SelectedObject = Config[e.Item.Text];
        }
        #endregion Event Handlers
    }
}
